package com.lenovo.leos.filebrowser.engine.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.Log;

import android.content.Context;
import android.content.res.Resources;

public class HzToPy {
	static private ArrayList<String> sortedHzList = null;
	static private ArrayList<String> sortedPyList = null;

	public HzToPy(Context context) throws IOException {
		if (sortedHzList == null) {
			Log.d("","********** MyHz2Py(): create the hanzi and pinyin list *******");
			Resources res = context.getResources();
			InputStream hz = res.openRawResource(R.raw.sortedhz);
			InputStreamReader hzReader = new InputStreamReader(hz);
			InputStream py = res.openRawResource(R.raw.sortedpy);
			InputStreamReader pyReader = new InputStreamReader(py);
			BufferedReader brHz = new BufferedReader(hzReader);// frHz);
			BufferedReader brPy = new BufferedReader(pyReader);// frPy);

			String sLine;
			sortedHzList = new ArrayList<String>();
			while ((sLine = brHz.readLine()) != null) {
				sortedHzList.add(sLine);
			}
			sortedPyList = new ArrayList<String>();
			while ((sLine = brPy.readLine()) != null) {
				sortedPyList.add(sLine);
			}
		}
	}
	
	public static String Char2Py(char c) throws UnsupportedEncodingException {
		String sChar = String.valueOf(c);
		if(sChar.getBytes("UTF-8").length == 1)
			return sChar.toLowerCase();
		
		String strch = null;
		int nIndex1 = 0;
		int nIndex2 = sortedHzList.size() - 1;
		int nIndex = 0;
				
	   while (nIndex1 <= nIndex2) {
			nIndex = (nIndex2 + nIndex1) / 2;
			int mResult = sortedHzList.get(nIndex).compareTo(sChar);
			if (mResult < 0)
				nIndex1 = nIndex + 1;
			else if (mResult > 0)
				nIndex2 = nIndex - 1;
			else {
				if(strch == null)
					strch = "";
            strch += sortedPyList.get(nIndex);
				break;
			}
		}

	   if(strch == null)
		   return "";
	   
		return strch;
	}
	
	/** [improve] Hz range need to be confirmed, maybe use constant to indicate the range*/
	public static boolean isHzChar(char c) {
		if ((int)c < 0x4E00 || (int)c > 0x9FA5 )
			return false;
		else
			return true;
	}

	/*public String Hz2Py(String value) throws UnsupportedEncodingException {
		String strch = new String();
		int strlength = value.length();
		int nEngWordStartIndex = 0;
		int nEngWordEndIndex = 0;
		
		if(value.length() == value.getBytes("UTF-8").length)
			return value;
		
		for (int i = 0; i < strlength; i++) {
			String tempstr = value.substring(i, i + 1);
			int nUtf8Len = tempstr.getBytes("UTF-8").length;
			if (nUtf8Len == 1) {
				nEngWordEndIndex = i;
			}else if (nUtf8Len > 1) {
				int nIndex1 = 0;
				int nIndex2 = sortedHzList.size() - 1;
				int nIndex = 0;
				
				if(nEngWordEndIndex > nEngWordStartIndex){
					if(strch .length() == 0)
						strch += value.substring(nEngWordStartIndex, nEngWordEndIndex+1);
					else{
						strch += " " + value.substring(nEngWordStartIndex, nEngWordEndIndex+1);
					}
				}
				nEngWordStartIndex = i+1;
				
				while (nIndex1 <= nIndex2) {
					nIndex = (nIndex2 + nIndex1) / 2;
					if (sortedHzList.get(nIndex).compareTo(tempstr) < 0)
						nIndex1 = nIndex + 1;
					else if (sortedHzList.get(nIndex).compareTo(tempstr) > 0)
						nIndex2 = nIndex - 1;
					else {
						if(strch.length() == 0)
                      strch += sortedPyList.get(nIndex);
                  else
                      strch +=" " + sortedPyList.get(nIndex);
						break;
					}
				}
			}
		}
		
		if(nEngWordStartIndex < strlength){
			if(strch .length() == 0)
				strch += value.substring(nEngWordStartIndex, strlength).toLowerCase();
			else{
				strch += " " + value.substring(nEngWordStartIndex, strlength).toLowerCase();
			}
		}

		return strch;
	}*/
}